define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pro/orderpointgoods/index',
                    add_url: 'pro/orderpointgoods/add',
                    edit_url: 'pro/orderpointgoods/edit',
                    // del_url: 'pro/orderpointgoods/del',
                    multi_url: 'pro/orderpointgoods/multi',
                    table: 'gzns_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                  if (!params.filter) {
                    params.filter={}
                  }else {
                    params.filter = JSON.parse(params.filter)
                  }
                  if (!params.op) {
                    params.op={}
                  }else {
                    params.op = JSON.parse(params.op)

                  }
                  if (!params.filter.order_type && !params.op.order_type) {
                    params.filter.order_type = '3';
                    params.op.order_type = '='
                  }
                  // if (!params.filter.order_status && !params.op.order_status) {
                  //   params.filter.order_status = '1,2';
                  //   params.op.order_status = 'IN'
                  // }
                  params.filter = JSON.stringify(params.filter)
                  params.op = JSON.stringify(params.op)

                  return params;
                },
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'order_number', title: __('Order_number')},
                        {field: 'order_status', title: __('Order_status'), title: __('Order_status'), searchList: {"1":__('Order_status 1'),"2":__('Order_status 2'),"3":__('Order_status 3')}, formatter: Table.api.formatter.status},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'user.nick_name', title: __('User.nick_name'),operate:'Like'},
                        // {field: 'user.real_name', title: __('User.real_name'),operate:'Like'},
                        // {field: 'user.phone', title: __('手机号'),operate:'Like'},

                        // {field: 'complete_time', title: __('Complete_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'close_time', title: __('Close_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'pay_type', title: __('Pay_type')},
                        // {field: 'user_message', title: __('User_message')},
                        // {field: 'order_type', title: __('Order_type')},
                        // {field: 'appoint_time', title: __('Appoint_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'cancel_reason', title: __('Cancel_reason')},
                        // {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        // {field: 'actual_price', title: __('Actual_price'), operate:'BETWEEN'},
                        // {field: 'refused_price', title: __('Refused_price'), operate:'BETWEEN'},
                        // {field: 'cancel_type', title: __('Cancel_type')},
                        // {field: 'wx_card_code', title: __('Wx_card_code')},
                        // {field: 'ticket_code_path', title: __('Ticket_code_path')},
                        // {field: 'user_vip_apply', title: __('User_vip_apply')},
                        // {field: 'master_id', title: __('Master_id')},
                        // {field: 'serial_number', title: __('Serial_number')},
                        {field: 'item.name', title: __('Item.name'),operate:'Like'},
                        {field: 'item.img', title: __('Item.img'), formatter: Table.api.formatter.image,operate:false},
                        {field: 'item.point', title: __('Item.point'), operate:'BETWEEN'},
                        {field: 'item.quantity', title: __('Item.quantity'), operate:'BETWEEN'},
                        {field: 'user_name', title: __('User_name')},
                        {field: 'user_phone', title: __('User_phone')},
                        {field: 'user_address', title: __('User_address')},
                        // {field: 'total_point', title: __('Total_point')},
                        {field: 'admin_cancel_reason', title: __('Admin_cancel_reason')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                      {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
              $(".btn-editone,.btn-edit,.btn-add").data("area", ["100%", "100%"]);
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
          Controller.api.bindevent();
          $('[data-status]').click(function () {
            var status = $(this).attr('data-status')
            $('#c-order_status').val(status)
            var tip = '确认吗'
            if(status == 2){
              tip = '确定设为已完成？'
            } else if(status == 3){
              tip = '确认取消订单？'
            }
            Layer.confirm(tip, {icon: 3, title:'提示'}, function(index){
              //do something
              $("form[role=form]").submit()
              Layer.close(index);
            });
            //
          })

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
